package drawables;



import java.awt.Color;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;

/**
 * 
 * @author David X. Wang
 *
 */
public class GlowFilter implements BufferedImageOp {
	
	private float hue;
	private int baseColor;
	
	public void setHue(float hue){
		this.hue = hue;
	}
	
	public void setBaseColor(int base){
		this.baseColor = base;
	}
	
	@Override
	public BufferedImage filter(BufferedImage src, BufferedImage dest) {
		if(dest == null)
			dest = createCompatibleDestImage(src, src.getColorModel());
		int[] inPixels = EffectUtils.getPixels(src);
		BrightnessContrastFilter filter = new BrightnessContrastFilter();
		filter.setBrightness(25);
		filter.setContrast(100);
		src = filter.filter(src, null);
		int[] blurPixels1 = EffectUtils.getPixels(new GaussianBlurFilter(5, 5).filter(src, null));
		int[] blurPixels2 = EffectUtils.getPixels(new GaussianBlurFilter(10, 10).filter(src, null));
		int[] blurPixels3 = EffectUtils.getPixels(new GaussianBlurFilter(20, 20).filter(src, null));
		int[] blurPixels4 = EffectUtils.getPixels(new GaussianBlurFilter(40, 40).filter(src, null));
		int[] outPixels = EffectUtils.getPixels(dest);
		for(int i=0; i<inPixels.length; i++){
			float[] hsb = EffectUtils.screenPixels(inPixels[i], blurPixels1[i], blurPixels2[i], blurPixels3[i], blurPixels4[i]);
			hsb[0] = hue;
			hsb[1] = 1f;
			hsb[2] *= 2f;
			if(hsb[2] > 1)
				hsb[2] = 1;
			outPixels[i] = EffectUtils.addPixels(baseColor, inPixels[i], Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
		}
		EffectUtils.setPixels(outPixels, dest);
		return dest;
	}
	
	@Override
	public Rectangle2D getBounds2D(BufferedImage src) {
		return new Rectangle(src.getWidth(), src.getHeight());
	}

	@Override
	public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) {
		return new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
	}

	@Override
	public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
		return null;
	}

	@Override
	public RenderingHints getRenderingHints() {
		return null;
	}

}
